<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: sap<songaiping@hrbjjhh.com>
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
 * @todo 品类管理
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2015-8-17
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;
class ConfCategoryController extends AdminController{
	
	/**
	 * @todo 品类管理列表
	 * @author sap
	 */
	public function index(){
		$search_text = strip_tags(I('search_text'));//搜索内容
		$map = array();//初始化查询条件
		//查询条件
		if($search_text != '' && $search_text != null){
			$map['conf_category'] = array('like', '%'.(string)$search_text.'%');
		}
		//列表
		$map['status']=array('gt',-1);
		$list = $this->lists('ConfCategory',$map,'sort desc, conf_category_id asc','conf_category_id,conf_category,sort,is_show');
		int_to_string($list,array('is_show'=>array(
		    '0'=>'隐藏',
		    '1'=>'显示'
		)));
		$this->assign('_list',$list);
		$this->assign('meta_title','品类管理');
		$this->display('index');
	}
	
	/**
	 * @todo 添加品类
	 * @author sap
	 */
	public function add(){
		if(IS_POST){
			$this->update();
		}else{
			$this->assign('list',null);
			$this->assign('meta_title','新增品类');
			$this->assign('meta_title_c','品类管理');
			$this->display('add');
		}
	}
	
	/**
	 * @todo 编辑品类
	 * @author sap
	 */
	public function edit(){
		if(IS_POST){
			$this->update();
		}else{
			//编辑时，品类详细信息
			$conf_category_id = (int)I('conf_category_id');
			if($conf_category_id == 0){
				$this->error('请正确操作');
			}
			$info = D('ConfCategory')->confCategoryShow($conf_category_id,'conf_category_id,conf_category,sort,is_show');
			//显示
			$this->assign('list',$info);
			$this->assign('meta_title','更新品类');
			$this->assign('meta_title_c','品类管理');
			$this->display('add');
		}
	}
	
	/**
	 * @todo 添加/编辑品类(仅供内部调用)
	 * @author sap
	 */
	private function update(){
	    $conf_category_id = (int)I('conf_category_id');
		$obj = D('ConfCategory');
		$id = $obj->update();
		if(empty($id)){
			$this->error($obj->getError());
		}
		//记录行为
		if($conf_category_id == 0){
		    action_log('conf_category_add', 'conf_category', $id, UID);
		}else{
		    action_log('conf_category_edit', 'conf_category', $conf_category_id, UID);
		}
		$this->success('操作成功',U('ConfCategory/index'));
	}
	
	/**
	 * @todo 品类状态修改
	 * @param string $method 标识
	 * @author sap
	 */
	public function changeStatus($method=null){
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if(empty($id)) {
			$this->error('请选择要操作的数据!');
		}
		$map[D('ConfCategory')->getPk()] = array('in',$id);
		switch ( strtolower($method) ){
			case 'forbiduser':
				$this->forbid('ConfCategory', $map );
				break;
			case 'resumeuser':
				$this->resume('ConfCategory', $map );
				break;
			case 'deleteuser':
				//判断品类下是否有店铺
				$obj_shop = D('Admin/Shop');
				if(!$obj_shop->judgeConfCategoryShop($id)){
					$this->error($obj_shop->getError());
				}
				$this->delete('ConfCategory', $map );
				//记录行为
				action_log('conf_category_del', 'conf_category', $id, UID);
				//删除缓存
				S('sys_conf_category_', null);
				break;
			default:
				$this->error('参数非法');
		}
	}

}